Welcome back.
We're going to see a little shorthand that makes it even easier to work
with files and avoids the need to remember to close them.
Python has an advanced feature called context managers.
We don't need to worry about all of its generality but
it makes possible this nice recipe for working with files.
It's easiest to understand it by example.
You start with a special word "with" and then there's another special word "as".
In between, you have the open statement.
Then we say, open in the file name and whether you want to read or write it.
After the word "as" you have a variable name.
So, this is actually equivalent to saying,
md equals open of
my data dot txt for reading.
It's equivalent to that in that in the code block that's indented under the word "with",
we can refer to md and md will be bound to our file object.
But we get a behind the scenes action that happens at the end of the code block.
It's as if we have an md dot close.
That gets executed behind the scenes.
We don't actually have to specify it,
it just gets done for us after everything else gets executed in that code block.
If I run this,
I just get a print
out of the first line and the second line.
As you might expect,
since we've iterated through the lines in the file object,
printing each one, we get the printout of those two lines that were in the file.
They're showing up in the output window.
Let's get rid of our markings here and just generalize this a little bit.
Suppose we didn't want to just open my data dot text but we
wanted this to work with any filename.
So let's say fname equals my data dot text.
We will open the file name here and then we would do stuff with the file object.
So, we would have md dot read or md
dot read lines or we might have for line in md.
Do something with each line.
So now we have sort of a general recipe for reading data from a file.
We start with this "with" statement where we open a file object and we assign it to
the variable name md and then we use one of our three methods of working with the file.
We either read all of its data in as a single character string with
dot read or we read all of the lines as a list of character strings
with dot read lines or we just iterate through those character strings
directly using iteration over the file object on line five.
The same recipe will work just as well for writing files.
So, if I change that r to a w,
I can do the same things that we would do for writing to a file.
So, for example, I might say for number in range of 10,
md dot write of
str of num and md dot write,
a new line character.
Again, the close happens automatically for us.
So, when I run this, and unfortunately,
I'm not seeing the file contents and that reminds
me of one little got you that you might want to remember.
If you're trying to write a file,
you can't write to a file that's the same name as one of the built-in files in the page.
We have a little protective measure that keeps you from overwriting it.
So, if I just change the name of the file,
now I get it as my output, what I was expecting.
You can survive a long time as a Python programmer not using the with construct.
But experienced programmers will use it.
You'll see it on stack overflow and other help sites.
So, it's a good idea to be able to read it and feel free to use it if you like.
See you next time.